1294D - MEX maximizing - CodeForces Solution


data structures greedy implementation math *1600

Please click on ads to support us..

Python Code:

n, x = map(int, input().split())
arr, result  = [0]*(n), []
mex = 0
for _ in range(n):
    arr[int(input())%x] += 1
    while arr[mex%x]:
        arr[mex%x] -= 1
        mex +=1
    result.append(mex)

print("\n".join(map(str, result)))

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <random>
#include <algorithm>
#include <unordered_map>
#include <map>
using namespace std;

#define int long long

vector<int> smt;

void update(int ver, int nl, int nr, int ind) {
    if (nr - nl == 1) {
        smt[ver] = 1;
        return;
    }
    int nm = nl + (nr - nl) / 2;
    if (ind < nm) update(ver * 2, nl, nm, ind);
    if (ind >= nm) update(ver * 2 + 1, nm, nr, ind);
    smt[ver] = smt[ver * 2] + smt[ver * 2 + 1];
}

int get(int ver, int nl, int nr) {
    if (nr - nl == 1) {
        return nl;
    }
    int nm = nl + (nr - nl) / 2;
    if (smt[ver * 2] == nm - nl) {
        return get(ver * 2 + 1, nm, nr);
    } else {
        return get(ver * 2, nl, nm);
    }
}

void solve() {
    int q, x;
    cin >> q >> x;
    vector<int> ost(x, 0);
    smt.assign(4*(q + 10), 0);
    for (int i = 0; i < q; ++i) {
        int y;
        cin >> y;
        y = y%x + x * ost[y%x];
        ost[y%x]++;
        update(1, 0, (q + 10), y);
        cout << get(1, 0, (q + 10)) << "\n";
    }
}



signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh